package com.zlj.request.encoding;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *  演示请求编码
 */
@WebServlet("/EncodServlet")
public class EncodServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
  
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name = request.getParameter("username");
		
		/*
		 * tomcat 8以后，编码方式设置为utf-8,不是以前的iso-8859-1,所以不需要如下的两个编码进行设置
		 */
		/*
			byte[] b = name.getBytes("iso-8859-1");
			name = new String(b,"utf-8");
		*/
		System.out.println(name);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 request.setCharacterEncoding("utf-8");
		 System.out.println(request.getParameter("username"));
	}
}
